package cn.xianshu.org.service.impl;

import cn.xianshu.basic.mapper.BasicMapper;
import cn.xianshu.basic.query.BasicQuery;
import cn.xianshu.basic.service.impl.BasicServiceImpl;
import cn.xianshu.basic.util.PageList;
import cn.xianshu.org.domain.Department;
import cn.xianshu.org.mapper.DepartmentMapper;
import cn.xianshu.org.query.DepartmentQuery;
import cn.xianshu.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
//@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BasicServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /*@Override
    public Department findOne(Long id) {
        return departmentMapper.loadById(id);
    }

    @Override
    public List<Department> findAll() {
        return departmentMapper.loadAll();
    }*/

    @Override
    @Transactional
    public void insert(Department department) {
        departmentMapper.insert(department); // 返回自增id
        // 两种情况
        // 添加的是顶级部门，dirPath = 自己的id
       if(department.getParent() == null){ // 顶级部门
            department.setDirPath("/" + department.getId());
        } else { // 添加的不是顶级部门， dirPath = 上级部门的dirParh +/+自己的id
           Department parentDept = departmentMapper.loadById(department.getParent().getId());
            String pdirPath = parentDept.getDirPath();
            department.setDirPath(pdirPath + "/" + department.getId());
        }
        // 更新
        departmentMapper.update(department);

    }

    @Override
    @Transactional
    public void update(Department department) {
        // 两种情况
        // 添加的是顶级部门，dirPath = 自己的id
        if(department.getParent() == null || department.getParent().getId() == null){ // 顶级部门
            department.setDirPath("/" + department.getId());
        } else { // 添加的不是顶级部门， dirPath = 上级部门的dirParh +/+自己的id
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            String dirPath = parentDept.getDirPath();
            department.setDirPath(dirPath + "/" + department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    public PageList<Department> queryList(BasicQuery query) {
        // 总条数
        Integer totals = departmentMapper.count(query);
        // 当前页数据
        List<Department> rows = departmentMapper.queryData(query);
        return new PageList<>(totals, rows);
    }

    /*@Override
    @Transactional
    public void delete(Long id) {
        departmentMapper.remove(id);
    }*/


    /*@Override
    public void Bydelete(List<Long> ids) {
        departmentMapper.bydelete(ids);
    }*/

    @Override
    public List<Department> queryTree() {
        return departmentMapper.loadTree();
    }
}
